/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.test.mock;

import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * <b>(ServiceTestMockContextThreadLocal)</b>
 * 用于Mock 测试的ThreadLocal
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/5/4
 */
@Getter
@NoArgsConstructor
public class ServiceTestMockContextThreadLocal implements AutoCloseable {
    static final ThreadLocal<ServiceTestMockContext> MOCK_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();

    public ServiceTestMockContextThreadLocal(ServiceTestMockContext context) {
        MOCK_CONTEXT_THREAD_LOCAL.set(context);
    }

    public static ServiceTestMockContext currentContext() {
        return MOCK_CONTEXT_THREAD_LOCAL.get();
    }

    public static void destroy() {
        MOCK_CONTEXT_THREAD_LOCAL.remove();
    }

    @Override
    public void close() {
        MOCK_CONTEXT_THREAD_LOCAL.remove();
    }
}